﻿<%@ WebHandler Language="C#" Class="CourseHandler" %>

using System;
using System.Web;
using System.Collections.Generic;

using EHSubmit.Logic;
using EHSubmit.Logic.Relationship;
using EHSubmit.Model;
using EHSubmit.Util;

public class CourseHandler : BaseAjaxHandler
{
    protected override void Mapping()
    {
        OprationMap["List_Course"] = "FindCourses";                               //列出老师或学生的课程
        OprationMap["List_TeacherBaseCourse"] = "FindTeacherBaseCourse";          //列出老师曾教过的课程，用于查找老师曾在某类课程布置过的习题
    }
    
    /// <summary>
    /// 列出老师或学生的课程
    /// </summary>
    public void FindCourses()
    {
        if (Validate(ERole.Teacher))
        {
            IRelationshipManager manager = RelationshipFactory.GetInstance();

            IList<Course> courses = manager.GetCoursesOfTeacher(CurrentUser.ID);

            string jsonCourses = JsonHelper.Serialize(courses);
            //if (jsonCourses == "")
            //{
            //    jsonCourses = "{Id:\"12012001\",Name:\"市场营销\"},{Id:\"12014002\",Name:\"人力资源管理\"}";
            //}
            IsSuccess = true;
            ResponseText = "{" + string.Format("courses:{0}", jsonCourses) + "}";
        }
        else if (Validate(ERole.Student))
        {
            IRelationshipManager manager = RelationshipFactory.GetInstance();

            IList<Course> courses = manager.GetCoursesOfStudent(CurrentUser.ID);

            string jsonCourses = JsonHelper.Serialize(courses);

            IsSuccess = true;
            ResponseText = "{" + string.Format("courses:{0}", jsonCourses) + "}";
        }
        else
        {
            Message = Constants.INFO_NO_ACCESS;
        }
    }    
    
    /// <summary>
    /// 列出老师曾教过的课程，用于查找老师曾在某类课程布置过的习题
    /// </summary>
    public void  FindTeacherBaseCourse()  
    {
        if(Validate(ERole.Teacher))
        {
            BaseCourseManager bcManager = new BaseCourseManager();
            IList<BaseCourse> baseCourseList = bcManager.GetBaseCourseList(CurrentUser.ID);

            string jsonBaseCourse = null;
            if (baseCourseList != null)
            {
                jsonBaseCourse = JsonHelper.Serialize(baseCourseList);
            }

            IsSuccess = true;
            ResponseText = "{" + string.Format("baseCourses:{0}", jsonBaseCourse) + "}";
        }
        else
        {
            Message = Constants.INFO_NO_ACCESS;
        }
    }
}